package com.thkj.modules.user.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.thkj.common.util.RequestUtil;
import com.thkj.modules.user.entity.TblIndividual;
import com.thkj.modules.user.service.TblIndividualService;

import dp.R;
import dp.dao.PagingList;
import dp.util.DateUtil;
import dp.util.UUIDUtil;
import dp.web.BaseAction;

/**
 * 经营户Action
 */
@RestController
@RequestMapping("/backstage/individual")
public class IndividualAction extends BaseAction {
    @Autowired
    TblIndividualService tblIndividualService;

    /**
     * 经营户列表
     *
     * @return
     */
    @RequestMapping("")
    public ModelAndView index() {
        return new ModelAndView("individual/list");
    }

    @RequestMapping("/query")
    public R query(HttpServletRequest request, int page, int size) {
        Map<String, Object> param = RequestUtil.getParameterMap(request);
        PagingList<Map> list = tblIndividualService.query(page, size, param);
        return R.success(list);
    }

    @RequestMapping("/apply")
    public ModelAndView apply() {
        return new ModelAndView("individual/apply");
    }

    /**
     * 审核
     */
    @RequestMapping("/check")
    @Transactional
    public R check(@RequestParam("id") String id, @RequestParam("state") String state) {
        Map<String, String> param = new HashMap<>();
        param.put("id", id);
        param.put("state", state);
        try {
            tblIndividualService.updateState(param);
            return R.SUCCESS;
        } catch (Exception e) {
            return R.failure(e.getMessage());
        }
    }

    @RequestMapping("/edit")
    @Transactional
    public R edit(TblIndividual individual) {
        try {
            if (StringUtils.isBlank(individual.getId())) {
                individual.setId(UUIDUtil.randomUUID());
                individual.setCreateTime(DateUtil.getNowAsTimestamp());
                tblIndividualService.insertTblIndividual(individual);
            } else {
                tblIndividualService.updateTblIndividual(individual);
            }
            return R.SUCCESS;
        } catch (Exception e) {
            return R.failure(e.getMessage());
        }
    }

    @RequestMapping("/delete")
    public R delete(String id) {
        tblIndividualService.deleteTblIndividual(id);
        return R.SUCCESS;
    }


}